﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;

namespace Warensoft.Unity.Communication.Client.DataClient
{
	public class DataProperty
	{
        public string ColumnName { get; set; }
        public object OriginalValue { get; set; }
        public object Value { get; set; }
        private bool isPrimaryKey;

        public bool IsPrimaryKey
        {
            get { return isPrimaryKey; }
            set { isPrimaryKey = value; }
        }

        public DataProperty(string columnName, object value)
        {
            this.ColumnName = columnName;
            this.Value = value;
            this.OriginalValue = value;
            this.isPrimaryKey = false;
        }
        public DataProperty(string columnName, object value, bool isPrimaryKey)
        {
            this.ColumnName = columnName;
            this.Value = value;
            this.OriginalValue = value;
            this.isPrimaryKey = isPrimaryKey;
        }
        public static DataProperty Create(byte[] buffer)
        {
            DataProperty property = null;
            using (MemoryStream ms=new MemoryStream (buffer))
            {
                using (BinaryReader br=new BinaryReader (ms))
                {
                    string columnName = br.ReadString();
                    bool isPrimaryKey = br.ReadBoolean();
                    object value = null;
                    object originalValue = null;
                    int originalValueBufferLength = br.ReadInt32();
                    if (originalValueBufferLength!=0)
                    {
                        BinaryFormatter bf = new BinaryFormatter();
                        byte[] valueBuffer = new byte[originalValueBufferLength];
                        br.Read(valueBuffer,0,valueBuffer.Length);
                        MemoryStream ms1 = new MemoryStream(valueBuffer);
                        originalValue = bf.Deserialize(ms1);
                        ms1.Close();
                    }
                    int valueBufferLength = br.ReadInt32();
                    if (valueBufferLength != 0)
                    {
                        BinaryFormatter bf = new BinaryFormatter();
                        byte[] valueBuffer = new byte[valueBufferLength];
                        br.Read(valueBuffer, 0, valueBuffer.Length);
                        MemoryStream ms1 = new MemoryStream(valueBuffer);
                        value = bf.Deserialize(ms1);
                        ms1.Close();
                    }
                    property = new DataProperty(columnName, value) 
                    {
                        IsPrimaryKey =isPrimaryKey,
                        OriginalValue=originalValue
                    };
                }
            }
            return property;
        }
        public byte[] ToArray()
        {
            byte[] buffer = null;
            using (MemoryStream ms=new MemoryStream ())
            {
                using (BinaryWriter bw=new BinaryWriter (ms))
                {
                    bw.Write(this.ColumnName);
                    bw.Write(this.IsPrimaryKey );
                    if (this.OriginalValue==null )
                    {
                        bw.Write((int)0);
                    }
                    else
                    {
                        BinaryFormatter bf = new BinaryFormatter();
                        MemoryStream ms1 = new MemoryStream();
                        bf.Serialize(ms1, this.OriginalValue);
                        var originalValueBuffer = ms1.ToArray();
                        ms1.Close();
                        bw.Write(originalValueBuffer.Length );
                        bw.Write(originalValueBuffer,0,originalValueBuffer .Length );
                    }
                    if (this.Value ==null )
                    {
                        bw.Write((int)0);
                    }
                    else
                    {
                        BinaryFormatter bf = new BinaryFormatter();
                        MemoryStream ms1 = new MemoryStream();
                        bf.Serialize(ms1, this.Value);
                        var valueBuffer = ms1.ToArray();
                        ms1.Close();
                        bw.Write(valueBuffer.Length);
                        bw.Write(valueBuffer, 0, valueBuffer.Length);
                    }
                   
                    
                }
                buffer = ms.ToArray();
            }
            return buffer;
        }
	}
}
